﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace phudieuhungthinh.Helpers
{
    public class HtmlTextWriterHelper : HtmlTextWriter
    {
        public HtmlTextWriterHelper(HtmlTextWriter writer)
            : base(writer)
        {
        }

        public IDisposable RenderTag(HtmlTextWriterTag tag)
        {
            this.RenderBeginTag(tag);
            return EndAction(() => this.RenderEndTag());
        }

        private IDisposable EndAction(Action end)
        {
            return new NestedInstruction(end);
        }

        protected class NestedInstruction : IDisposable
        {
            private readonly Action endAction;

            public NestedInstruction(Action end)
            {
                endAction = end;
            }

            public void Dispose()
            {
                endAction();
            }
        }

        public void AddId(string value)
        {
            base.AddAttribute(HtmlTextWriterAttribute.Id, value);
        }

        public void AddClass(string value)
        {
            base.AddAttribute(HtmlTextWriterAttribute.Class, value);
        }
    }
}